import Vue from 'vue'
import VueRouter from 'vue-router'
import Homepage from "../views/Homepage";
import Index from "../views/Index"
import Backgroud from "../views/Backgroud";
import Information from "../views/Information";
import Creat from "../views/Creat";
import Change from "../views/Change";
import dataShow from '../views/DataShow.vue';


import login from "../views/login";
import Preview from "../views/Preview";
import Answer from "../views/Answer";
import paperDone from '../views/paperDone.vue';
import finish from '../views/Finish.vue';
import checkAnswers from '../views/CheckAnswers.vue';
import AnswerPreview from '../views/AnswerPreview';
import TeacherCheck from "../views/TeacherCheck";
import Search from "../views/Search";
import SearchExam from "../views/SearchExam";
import SearchPaper from "../views/SearchPaper";
import CreateSet from "../views/CreateSet";
import MobileBar from "../views/MobileBar";
import MobileHome from "../views/MobileHome";
import paperDoing from "../views/paperDoing";
import LoginMobile from "../views/LoginMobile";

Vue.use(VueRouter)

const routes = [
  {
    path:'/loginMobile',
    name:"loginMobile",
    component: LoginMobile
  },
  {
    path: '/background',
    name: '登录',
    component: Backgroud,
    children: [
      {
        path:'/login',
        name:"登录",
        component: login
      }
    ]
  },
  {
    path:'/answer/:id',
    name:'答题',
    component:Answer,
    meta:{
      auth:true
    }
  },
  {
    path:'/mobileHome',
    name:'主页',
    component:MobileHome,
    meta:{
      auth:true
    }
  },
  {
    path:'/answerPreview',
    name:'答题查看',
    component:AnswerPreview
  },
  {
    path: "/preview",
    name:"预览",
    component: Preview,
    meta:{
      auth:true
    }
  },
  {
	  path:'/finish',
	  name:'结束',
	  component:finish
  },
  {
    path: "/createSet",
    name:"设置",

    component: CreateSet,
    meta:{
      auth:true
    }
  },
  {
    path: "/creat",
    name:"创建试卷",
    component: Creat,
    meta:{
      auth:true
    }
  },
  {
    path: "/mobileBar",
    name:"导航栏",
    component: MobileBar,
    meta:{
      auth:true
    }
  },
  {
    path:"/",
    name:"首页",
    component:Index,
    show:true,
    redirect:"/homepage",
    children:[

      {
        path:"/homepage",
        name:"我的试卷",
        component:Homepage,
        meta:{
          auth:true
        }
      },
      {
        path: "/paperDoing",
        name:"待做试卷",
        component: paperDoing,
        meta:{
          auth:true
        }
      },
      {
        path: "/paperDone",
        name:"已做试卷",
        component: paperDone,
        meta:{
          auth:true
        }
      }
  ]
  },
  {
    path:"/",
    name:"首页",
    component:Index,
    redirect:"/homepage",
    children:[
        {
		  path: "/dataShow",
		  name: "dataShow",
		  component: dataShow,
          meta:{
            auth:true
          }
		},
      {
        path: "/teacherCherk",
        name: "teacherCherk",
        component: TeacherCheck,
        meta:{
          auth:true
        }
      },

      {
        path: "/information",
        name:"个人信息",
        component: Information,
        meta:{
          auth:true
        }
      },


      {
        path: "/change",
        name:"修改试卷",
        component: Change,
        meta:{
          auth:true
        }
      },
	  {
	    path: "/checkAnswers",
	    name:"批改试卷",
	    component: checkAnswers,
	    meta:{
	      auth:true
	    }
	  },
      {
        path: "/search",
        name:"搜索",
        component: Search,
        meta:{
          auth:true
        },
        children:[
          {
            path: "/searchexam",
            name:"搜索试卷",
            component: SearchExam,
            meta:{
              auth:true
            }
          },
          {
            path: "/searchpaper",
            name:"搜索试卷",
            component: SearchPaper,
            meta:{
              auth:true
            }
          }
        ]

      }
    ]
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{

  if(to.meta.auth){
    if(sessionStorage.getItem('id')){
      next();
    }else {
      next({
        path:'login',
        query:{
          redirect:to.fullPath,
        }
      })
    }
  }else {
    next()
  }
})

export default router
